package org.xworker.model;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;

import java.lang.reflect.Method;

public class ModelMain {
    public static void main(String[] args) {
        try {
            World world = World.getInstance();

            world.init("./xworker");

            ActionContext actionContext = new ActionContext();

            actionContext = new ActionContext();
            Thing testAction = world.getThing("org.xworker.model.TestMyAction");
            System.out.println("1+2=" + testAction.getAction().run(actionContext, "x", 1, "y", 2));

            int count = 1000000;
            long start = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                testAction.getAction().run(actionContext, "x", 1, "y", 2);
            }
            System.out.println("执行" + count + "耗时：" + (System.currentTimeMillis() - start));

            start = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                world.getThing("org.xworker.model.TestMyAction");
            }
            System.out.println("执行getThing()" + count + "耗时：" + (System.currentTimeMillis() - start));

            start = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                add(1, 2);
            }
            System.out.println("执行java:add(1, 2)" + count + "耗时：" + (System.currentTimeMillis() - start));

            Method addMethod = ModelMain.class.getMethod("add", int.class, int.class);
            start = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                addMethod.invoke(null, 1, 2);
            }
            System.out.println("执行java reflect:add(1, 2)" + count + "耗时：" + (System.currentTimeMillis() - start));

            Thing testHtml = world.getThing("org.xworker.model.TestHtml");
            System.out.println("html=" + testHtml.doAction("toHtml", actionContext));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int add(int x , int y) {
        return x + y;
    }
}